package com.feri.gpstudy.service.impl;

import com.feri.gpstudy.bo.UserAddBo;
import com.feri.gpstudy.bo.UserWhereBo;
import com.feri.gpstudy.entity.User;
import com.feri.gpstudy.dao.UserDao;
import com.feri.gpstudy.service.intf.UserService;
import com.feri.gpstudy.vo.R;
import javax.annotation.Resource;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 千锋郑州Java学科欢迎你
 * @program: GpStudy
 * @description: 业务接口实现层
 * @author: Feri
 * @create: 2023-04-21 09:59:58
 */
@Service
public class UserServiceImpl implements UserService{
    /**
    *注入持久层对象*/
    @Resource
    private UserDao dao;

    /**
    * 新增内容*/
    @Override
    public R save(UserAddBo bo) {
        User user=new User();
        BeanUtils.copyProperties(bo,user);
        if(user !=null){
            int i=dao.insert(user);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("亲，新增异常！");
    }
    /**
    * 修改内容*/
    @Override
    public R update(User user) {
        if(user !=null) {
            int i = 0;
            i = dao.update(user);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，修改异常！");
    }
    /**
     *删除 */
    @Override
    public R del(long id){
        if(id>0){
            int i=dao.del(id);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
     * 查询详情*/
    @Override
    public R queryById(long id){
        return R.ok(dao.selectById(id));
    }

    /**
     * 查询 所有数据*/
    @Override
    public R queryAll(UserWhereBo bo) {
        PageHelper.startPage(bo.getPage(),bo.getSize());
        PageInfo<User> pageInfo=new PageInfo<>(dao.selectAll(bo.getUsername(),bo.getRealname(),bo.getRole()));
        return R.ok(pageInfo);
    }
}
